package com.yiren.dbaa.module.help_center.view.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiren.backstage.currency.bean.CarStateBean;
import com.yiren.backstage.currency.bean.GpsInfoBean;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.fragment.LazyLoadFragment;
import com.yiren.dbaa.module.device.bean.DeviceBaseInfoBean;
import com.yiren.dbaa.module.device.bean.DeviceDisplayInfoBean;
import com.yiren.dbaa.module.device.bean.DeviceGjdInfoBean;
import com.yiren.dbaa.module.device.contract.DeviceInfoContract;
import com.yiren.dbaa.module.device.presenter.DeviceInfoPresenter;
import com.yiren.dbaa.module.device.view.activity.SerialPortDataActivity;
import com.yiren.dbaa.module.help_center.view.PointerLocationView;
import com.yiren.dbaa.module.practice_set.device.CarStateTextViewBean;
import com.yiren.dbaa.view.toast.MessageBox;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/7/12 14:40
 * @description: 设备信息
 */
public class DeviceInfoFragment extends LazyLoadFragment implements DeviceInfoContract.View {


    @BindView(R.id.tv_state_xcd)
    TextView tvStateXcd;
    @BindView(R.id.tv_state_zzd)
    TextView tvStateZzd;
    @BindView(R.id.tv_state_wd)
    TextView tvStateWd;
    @BindView(R.id.tv_state_js)
    TextView tvStateJs;
    @BindView(R.id.tv_state_aqd)
    TextView tvStateAqd;
    @BindView(R.id.tv_state_jgd)
    TextView tvStateJgd;
    @BindView(R.id.tv_state_yzd)
    TextView tvStateYzd;
    @BindView(R.id.tv_state_lh)
    TextView tvStateLh;
    @BindView(R.id.tv_state_cm)
    TextView tvStateCm;
    @BindView(R.id.tv_state_ygd)
    TextView tvStateYgd;
    @BindView(R.id.tv_state_jsd)
    TextView tvStateJsd;
    @BindView(R.id.tv_state_ss)
    TextView tvStateSs;
    @BindView(R.id.tv_state_lb)
    TextView tvStateLb;
    @BindView(R.id.tv_state_zq)
    TextView tvStateZq;
    @BindView(R.id.tv_state_yq)
    TextView tvStateYq;
    @BindView(R.id.tv_state_yh)
    TextView tvStateYh;
    @BindView(R.id.tv_state_zh)
    TextView tvStateZh;
    @BindView(R.id.tv_state_hsj)
    TextView tvStateHsj;
    @BindView(R.id.tv_state_fgj)
    TextView tvStateFgj;
    @BindView(R.id.tv_state_zwy)
    TextView tvStateZwy;
    @BindView(R.id.tv_state_zy)
    TextView tvStateZy;

    @BindView(R.id.tv_phone_number)
    TextView tvPhoneNumber;
    @BindView(R.id.tv_app_version)
    TextView tvAppVersion;
    @BindView(R.id.tv_hardware_version)
    TextView tvHardwareVersion;
    @BindView(R.id.ll_device_hardware_info)
    LinearLayout llDeviceHardwareInfo;
    @BindView(R.id.tv_hardware_version_gjd)
    TextView tvHardwareVersionGjd;
    @BindView(R.id.tv_device_number)
    TextView tvDeviceNumber;
    @BindView(R.id.tv_device_code)
    TextView tvDeviceCode;
    @BindView(R.id.tv_car_model)
    TextView tvCarModel;
    @BindView(R.id.tv_car_model_gjd)
    TextView tvCarModelGjd;
    @BindView(R.id.tv_device_connect)
    TextView tvDeviceConnect;
    @BindView(R.id.tv_net_work)
    TextView tvNetWork;
    @BindView(R.id.tv_screen_light)
    TextView tvScreenLight;
    @BindView(R.id.tv_voice_value)
    TextView tvVoiceValue;
    @BindView(R.id.ll_reflow)
    LinearLayout llReflow;
    @BindView(R.id.tv_sim_type)
    TextView tvSimType;
    @BindView(R.id.tv_active_time)
    TextView tvActiveTime;
    @BindView(R.id.tv_iccid)
    TextView tvIccid;
    @BindView(R.id.tv_total_flow)
    TextView tvTotalFlow;
    @BindView(R.id.tv_remain_flow)
    TextView tvRemainFlow;
    @BindView(R.id.tv_net_recog)
    TextView tvNetRecog;
    @BindView(R.id.tv_base_distance)
    TextView tvBaseDistance;
    @BindView(R.id.tv_base_name)
    TextView tvBaseName;
    @BindView(R.id.ll_gjd_info)
    LinearLayout llGjdInfo;
    @BindView(R.id.tv_car_speed)
    TextView tvCarSpeed;
    @BindView(R.id.tv_car_rotate)
    TextView tvCarRotate;
    @BindView(R.id.tv_car_gear)
    TextView tvCarGear;
    @BindView(R.id.tv_angle)
    TextView tvAngle;
    @BindView(R.id.tv_car_key)
    TextView tvCarKey;
    @BindView(R.id.tv_longitude)
    TextView tvLongitude;
    @BindView(R.id.tv_latitude)
    TextView tvLatitude;
    @BindView(R.id.tv_front_antenna)
    TextView tvFrontAntenna;
    @BindView(R.id.tv_after_antenna)
    TextView tvAfterAntenna;

    private DeviceInfoContract.Presenter deviceInfoPresenter;
    private GpsInfoBean gpsInfoBean;
    private CarStateBean carStateBean;
    private List<CarStateTextViewBean> carStateTextViewList;

    @Override
    protected int setContentView() {
        return R.layout.fragment_device_info;
    }

    @Override
    protected void init() {
        carStateTextViewList = new ArrayList<>();
        deviceInfoPresenter = new DeviceInfoPresenter(getActivity());
        deviceInfoPresenter.getBaseInfo(this);
        deviceInfoPresenter.getDisPlayInfo(this);
        deviceInfoPresenter.getGjdInfo(this);
        deviceInfoPresenter.getCarState(this);
    }



    @Override
    public void showBaseInfo(DeviceBaseInfoBean deviceBaseInfo) {
        tvPhoneNumber.setText("登录账号："+deviceBaseInfo.getPhoneNumber());
        tvAppVersion.setText("软件版本："+deviceBaseInfo.getAppVersion());
        if (deviceBaseInfo.isConectDevice()) {
            tvHardwareVersion.setText("硬件版本："+deviceBaseInfo.getHardWareVersion());
            tvDeviceNumber.setText("设备编号："+deviceBaseInfo.getDeviceNumber());
            tvDeviceCode.setText("设备芯片码："+deviceBaseInfo.getDeviceCode());
            tvCarModel.setText("车型："+deviceBaseInfo.getCarModel());
        } else {
            tvHardwareVersion.setVisibility(View.GONE);
            llDeviceHardwareInfo.setVisibility(View.GONE);
        }
    }

    @Override
    public void showDisplayInfo(DeviceDisplayInfoBean deviceDisplayInfo) {
        tvDeviceConnect.setText("设备连接："+deviceDisplayInfo.getDeviceConnect());
        tvNetWork.setText("网络："+deviceDisplayInfo.getNetWork());
        tvScreenLight.setText("屏幕亮度："+deviceDisplayInfo.getScreenLight());
        tvVoiceValue.setText("音量："+deviceDisplayInfo.getVoiceValue());
        tvSimType.setText("网络方式："+deviceDisplayInfo.getSimType());
        if (deviceDisplayInfo.getSimType().contains("卡")){
            tvIccid.setText("ICCID："+deviceDisplayInfo.getIccid());
            tvActiveTime.setText("激活时间："+deviceDisplayInfo.getActivateTime());
            tvRemainFlow.setText("当前剩余流量："+deviceDisplayInfo.getRemainNetFlow());
            tvTotalFlow.setText("年度总流量："+deviceDisplayInfo.getTotalNetFlow());
        }else {
            llReflow.setVisibility(View.GONE);
            tvIccid.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void showGjdInfo(DeviceGjdInfoBean deviceGjdInfo, boolean isShow) {
        if (isShow) {
            tvHardwareVersionGjd.setText("硬件版本："+deviceGjdInfo.getHardwareVersion());
            tvCarModelGjd.setText("当前使用车模："+deviceGjdInfo.getCarModel());
            tvNetRecog.setText("网络识别号："+deviceGjdInfo.getNetRecogData());
            tvBaseDistance.setText("固定站距离："+deviceGjdInfo.getBaseStationDistance());
            tvBaseName.setText("固定站："+deviceGjdInfo.getBaseStationName());
        } else {
            llGjdInfo.setVisibility(View.GONE);
        }
    }

    @Override
    public void showCarState() {
        carStateBean = CarStateBean.getInstance();
        gpsInfoBean = GpsInfoBean.getInstance();
        tvCarSpeed.setText("车速："+String.valueOf(carStateBean.getSpeed()));
        tvCarRotate.setText("转速："+String.valueOf(carStateBean.getRotateSpeed()));
        tvCarGear.setText("档位："+carStateBean.getGear().getName());
        tvAngle.setText("角度："+String.valueOf(carStateBean.getNsbsAngleDiff()));
        tvCarKey.setText("钥匙："+carStateBean.getCarKey().getName());
        DecimalFormat myformat=new java.text.DecimalFormat("0.000");
        tvLongitude.setText("经度："+String.valueOf(myformat.format(gpsInfoBean.getLongitude())));
        tvLatitude.setText("纬度："+String.valueOf(myformat.format(gpsInfoBean.getLatitude())));
        tvFrontAntenna.setText("前天线："+gpsInfoBean.getStatusDirection() + "/" + gpsInfoBean.getSatelliteFront());
        tvAfterAntenna.setText("后天线："+gpsInfoBean.getStatusPosition() + "/" + gpsInfoBean.getSatelliteBack());

        carStateTextViewList.clear();
        tvStateXcd.setText(carStateBean.getDrivingLamp().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateXcd, carStateBean.getDrivingLamp()));
        tvStateZzd.setText(carStateBean.getLeftLight().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateZzd, carStateBean.getLeftLight()));
        tvStateWd.setText(carStateBean.getFoglight().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateWd, carStateBean.getFoglight()));
        tvStateJs.setText(carStateBean.getFootBrake().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateJs, carStateBean.getFootBrake()));
        tvStateAqd.setText(carStateBean.getSafetyBelt().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateAqd, carStateBean.getSafetyBelt()));
        tvStateJgd.setText(carStateBean.getPassingLight().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateJgd, carStateBean.getPassingLight()));
        tvStateYzd.setText(carStateBean.getRightLight().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateYzd, carStateBean.getRightLight()));
        tvStateLh.setText(carStateBean.getSeparationReunion().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateLh, carStateBean.getSeparationReunion()));
        tvStateCm.setText(carStateBean.getCarDoor().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateCm, carStateBean.getCarDoor()));
        tvStateYgd.setText(carStateBean.getDistanceLight().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateYgd, carStateBean.getDistanceLight()));
        tvStateJsd.setText(carStateBean.getCautionLight().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateJsd, carStateBean.getCautionLight()));
        tvStateSs.setText(carStateBean.getParkingBrake().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateSs, carStateBean.getParkingBrake()));
        tvStateLb.setText(carStateBean.getHorn().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateLb, carStateBean.getHorn()));
        tvStateZq.setText(carStateBean.getCarHeadLeft().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateZq,carStateBean.getCarHeadLeft()));
        tvStateYq.setText(carStateBean.getCarHeadRight().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateYq,carStateBean.getCarHeadRight()));
        tvStateYh.setText(carStateBean.getCarTailRight().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateYh,carStateBean.getCarTailRight()));
        tvStateZh.setText(carStateBean.getCarTailLeft().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateZh,carStateBean.getCarTailLeft()));
        tvStateHsj.setText(carStateBean.getRearviewMirror().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateHsj,carStateBean.getRearviewMirror()));
        tvStateFgj.setText(carStateBean.getReflectorMirror().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateFgj,carStateBean.getReflectorMirror()));
        tvStateZwy.setText(carStateBean.getFingerprint().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateZwy,carStateBean.getFingerprint()));
        tvStateZy.setText(carStateBean.getSeat().getName());
        carStateTextViewList.add(new CarStateTextViewBean(tvStateZy,carStateBean.getSeat()));

        for (CarStateTextViewBean carStateTextView : carStateTextViewList) {
            String state = carStateTextView.getCarStateValueEnum().getName();
            if (state.contains("松开") || state.contains("放下") || state.contains("关闭") || state.contains("未通过") || state.contains("未调整") || state.contains("未触摸")) {
                carStateTextView.getTv().setTextColor(Color.parseColor("#999999"));
            } else if (state.contains("无信号")) {
            } else {
                carStateTextView.getTv().setTextColor(Color.parseColor("#22A146"));
            }
        }



    }


    @OnClick({R.id.tv_touch_screen_btn,R.id.tv_voice_btn,R.id.tv_port_data_btn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_touch_screen_btn:
                startActivity(new Intent(getActivity(), PointerLocationView.class));
                break;
            case R.id.tv_voice_btn:
                MessageBox.VoiceModel(getActivity(),"滴驾，让学车更简单！");
                break;
            case R.id.tv_port_data_btn:
                startActivity(new Intent(getActivity(), SerialPortDataActivity.class));
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        deviceInfoPresenter.removeAllCallBack();
    }
}
